import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
import { WeaDialog, WeaTools, WeaErrorPage } from 'ecCom';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
import FavouriteDir from './dir/index';
import SysFavourite from './favourite/index';
import '../style/index.css';
import SearchFavourite from '../util/SearchFavourite';

@inject('myFavouriteStore')
@inject('favouriteDirStore')
@inject('sysFavouriteStore')

@observer
class MyFavourite extends Component {
    constructor(props) {
      super(props);
      this.state = { visible: false };
    }

    open = () => {
      const { myFavouriteStore, favouriteDirStore, sysFavouriteStore } = this.props;
      // 页面初始化的一些数据
      myFavouriteStore.initPage();
      // 先将树原本的数据清空
      favouriteDirStore.initTree();
      // 加载左侧目录树的数据
      favouriteDirStore.loadTreedata('0');
      // favouriteDirStore.loadTree("0");
      // 初始化加载收藏内容第一页的数据
      const isinit = true;
      const searchFavourite = new SearchFavourite(sysFavouriteStore);
      searchFavourite.reset(); // 先初始化收藏的条件
      searchFavourite.load({ isinit });
      this.setState({ visible: true });
    }

    close = () => {
      this.setState({ visible: false });
    }

    render() {
      const { children, className } = this.props;
      const { visible } = this.state;

      const title = getLabel(2081, '收藏夹');
      const style = { width: 900, height: 650 };
      const onCancel = this.close;
      const params = { visible, title, style, onCancel };

      return (
        <div style={{ display: 'inline-block' }} onClick={this.open} className={className}>
          {children}
          <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@3vciqz`} {...params}>
            <FavouriteDir ecId={`${this && this.props && this.props.ecId || ''}_FavouriteDir@xzjman`} />
            <SysFavourite ecId={`${this && this.props && this.props.ecId || ''}_SysFavourite@qwc1vp`} />
          </WeaDialog>
        </div>
      );
    }
}

class MyErrorHandler extends React.Component {
  render() {
    const hasErrorMsg = this.props.error && this.props.error !== '';
    return (
      <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@5grb8k`} msg={hasErrorMsg ? this.props.error : `${getLabel(385218, '对不起，该页面异常，请联系管理员！')}`} />
    );
  }
}

MyFavourite = WeaTools.tryCatch(React, MyErrorHandler, { error: '' })(MyFavourite);

export default MyFavourite;
